package com.opensky.qatest.ui.page;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.common.Properties;
import com.opensky.qatest.util.junit.Assert;
import com.opensky.qatest.util.selenium.HTML;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Sale page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class SalesPage extends LoadableComponent<SalesPage> implements IPage {

    // ************************ Locators ************************

    private static final String SALES_PAGE_URL = Properties.get("env.root.url") + "sales";

    private static final String LOGOUT_URL = Properties.get("env.root.url") + "logout/";

    private static final String LOGO = "//div[@id='logo']//img";

    private static final String CURATOR_MNG_LINK = "//div[@id='sky-bar']//a/div[@class='add']";

    private static final String CART_NAV = "cart-nav";

    private static final String HELP_LINK = "//div[@id='sky-bar']//a[@href='/content/faq']";

    private static final String YOUR_SALES_LINK = "//header//span[@class='your-sales']/a";

    private static final String FOOD_CATEGORY = "//ul[@id='category-nav']/li/a[@href='https://opensky.com/curators/food']";

    private static final String STYLE_CATEGORY = "//ul[@id='category-nav']/li/a[@href='https://opensky.com/curators/style']";

    private static final String HEALTH_CATEGORY = "//ul[@id='category-nav']/li/a[@href='https://opensky.com/curators/healthy-living']";

    private static final String DESIGN_CATEGORY = "//ul[@id='category-nav']/li/a[@href='https://opensky.com/curators/home-design']";

    private static final String INVITE_FRIENDS_LINK = "//div[@id='invite-friends-nav']/a";

    private static final String CREDITS_LINK = "//div[@id='credits-nav']/a";

    private static final String ACCOUNT_DROPDOWN = "//div[@id='account-nav']/a[@class='dropdown-label']";

    private static final String WELCOME_MESSAGE_CONTAINER = "//div[@class='simple-title-header']";

    private static final String WELCOME_MESSAGE_TEXT = "Welcome, %s! Here are today's sales.";

    private static final String CURATOR_SALES_CATEGORY = "//div[@id='curator-sales']";

    private static final String OTHER_CURATOR_SALES_CATEGORY = "//div[@id='other-curator-sales']";

    private static final String UPCOMING_SALES = "//div[@id='upcoming-sales']";

    private static final String OFFER_IMG = "//ul[@class='sellable-list']/li[1]//img";

    private static final String OFFER_CURATOR = "//ul[@class='sellable-list']/li[1]//div[@class='curator-attribution']/a";

    private static final String OFFER_NAME = "//ul[@class='sellable-list']/li[1]//h3[@class='sellable-name']/a";

    private static final String OFFER_BTN_LEARN_MORE = "//ul[@class='sellable-list']/li[1]//div[@class='action']/a[@class='following']";

    private static final String EARN_CREDITS_BLOCK = "//div[@class='earn-credits']";

    private static final String FOOTER_NAV = "//footer//ul[@class='links']";

    private static final String CONTACT_PHONE_CONTAINER = "//footer//div[@class='contact-us']//div";

    private static final String CONTACT_PHONE_TEXT = "1 (877) 734-6736";

    private static final String CONTACT_EMAIL_CONTAINER = "//footer//div[@class='contact-us']//a";

    private static final String CONTACT_EMAIL_TEXT = "help@opensky.com";

    private static final String COPYRIGHT_CONTAINER = "//footer//div[@class='copyright']";

    private static final String COPYRIGHT_TEXT = "2011 The OpenSky Project, Inc.";

    private static final String NYC_LINK = "//footer//div[@id='made-in-nyc']/a";

    private final WebDriverWrapper driver;

    public SalesPage(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(SALES_PAGE_URL);
    }

    public LoginPage logOut() {
	driver.get(LOGOUT_URL);
	return new LoginPage(driver);
    }

    public SalesPage clickYourSalesLink() {
	driver.waitAndClick("YOUR_SALES_LINK", By.xpath(YOUR_SALES_LINK));

	SalesPage salesPage = new SalesPage(driver);

	if (salesPage.isPageLoaded()) {
	    return salesPage;
	} else {
	    return null;
	}
    }

    public OfferPage clickOfferLearnMoreBtn() {

	WebElement productLink = driver.waitAssertAndGetElement("OFFER_BTN_LEARN_MORE", By.xpath(OFFER_BTN_LEARN_MORE));
	String href = productLink.getAttribute(HTML.HREF);
	String curator = href.split("/")[3];
	String product = href.split("/")[5];

	OfferPage offerPage = new OfferPage(driver, curator, product);

	driver.waitAndClick("OFFER_BTN_LEARN_MORE", By.xpath(OFFER_BTN_LEARN_MORE));

	return offerPage.isPageLoaded() ? offerPage : null;
    }

    public SalesPage clickLogo() {

	driver.waitAndClick("LOGO", By.xpath(LOGO));
	return this.isPageLoaded() ? this : null;
    }

    // ************************ Validations *************************

    @Override
    protected void isLoaded() {
	Assert.assertUrlEqual(SALES_PAGE_URL, driver.getCurrentUrl());
    }

    @Override
    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(SALES_PAGE_URL);
    }

    public boolean isSkyBarBaseElementsPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("CURATOR_MNG_LINK", By.xpath(CURATOR_MNG_LINK)),
		driver.waitAndVerifyElementPresent("CART_NAV", By.id(CART_NAV)), driver.waitAndVerifyElementPresent("HELP_LINK", By.xpath(HELP_LINK)));
    }

    public boolean isTopNavBaseElemntsPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("YOUR_SALES_LINK", By.xpath(YOUR_SALES_LINK)),
		driver.waitAndVerifyElementPresent("FOOD_CATEGORY", By.xpath(FOOD_CATEGORY)),
		driver.waitAndVerifyElementPresent("STYLE_CATEGORY", By.xpath(STYLE_CATEGORY)),
		driver.waitAndVerifyElementPresent("HEALTH_CATEGORY", By.xpath(HEALTH_CATEGORY)),
		driver.waitAndVerifyElementPresent("DESIGN_CATEGORY", By.xpath(DESIGN_CATEGORY)),
		driver.waitAndVerifyElementPresent("INVITE_FRIENDS_LINK", By.xpath(INVITE_FRIENDS_LINK)),
		driver.waitAndVerifyElementPresent("CREDITS_LINK", By.xpath(CREDITS_LINK)),
		driver.waitAndVerifyElementPresent("ACCOUNT_DROPDOWN", By.xpath(ACCOUNT_DROPDOWN)));
    }

    public boolean isWelcomeMessagePresent(String userEmail) {

	String userName = userEmail.split("@")[0];

	return driver.waitAndVerifyElementWithTextPresent("WELCOME_MESSAGE_CONTAINER", By.xpath(WELCOME_MESSAGE_CONTAINER),
		String.format(WELCOME_MESSAGE_TEXT, userName));
    }

    public boolean isAllSalesCategoriesPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("CURATOR_SALES_CATEGORY", By.xpath(CURATOR_SALES_CATEGORY)),
		driver.waitAndVerifyElementPresent("OTHER_CURATOR_SALES_CATEGORY", By.xpath(OTHER_CURATOR_SALES_CATEGORY)),
		driver.waitAndVerifyElementPresent("UPCOMING_SALES", By.xpath(UPCOMING_SALES)));
    }

    public boolean isOfferBaseElementsPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("OFFER_IMG", By.xpath(OFFER_IMG)),
		driver.waitAndVerifyElementPresent("OFFER_CURATOR", By.xpath(OFFER_CURATOR)),
		driver.waitAndVerifyElementPresent("OFFER_NAME", By.xpath(OFFER_NAME)));
    }

    public boolean isRightSideBlocksPresent() {
	return driver.waitAndVerifyElementPresent("EARN_CREDITS_BLOCK", By.xpath(EARN_CREDITS_BLOCK));
    }

    public boolean isFooterBaseElementsPresent() {
	return LogicUtils.isAllTrue(driver.waitAndVerifyElementPresent("FOOTER_NAV", By.xpath(FOOTER_NAV)),
		driver.waitAndVerifyElementWithTextPresent("CONTACT_PHONE_CONTAINER", By.xpath(CONTACT_PHONE_CONTAINER), CONTACT_PHONE_TEXT),
		driver.waitAndVerifyElementWithTextPresent("CONTACT_EMAIL_CONTAINER", By.xpath(CONTACT_EMAIL_CONTAINER), CONTACT_EMAIL_TEXT),
		driver.waitAndVerifyElementWithTextPresent("COPYRIGHT_CONTAINER", By.xpath(COPYRIGHT_CONTAINER), COPYRIGHT_TEXT),
		driver.waitAndVerifyElementPresent("NYC_LINK", By.xpath(NYC_LINK)));
    }

    // ************************ GET/SET methods **************************

    @Override
    public String getName() {
	return "Sales page";
    }
}
